package com.ftw.commons.utils

import android.content.Context
import android.graphics.drawable.Drawable
import android.net.Uri
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.Priority
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.request.RequestOptions
import com.ftw.commons.R

/**
 * 描    述：
 * 作    者：fantianwen
 * 时    间：2019/3/3  4:04 PM
 */

object ImageUtils {

    /**
     * 加载圆形图片
     *
     * @param context
     * @param path
     * @param imageView
     */
    fun loadCircleImg(context: Context, path: String, imageView: ImageView) {

        val mRequestOptions = RequestOptions.circleCropTransform()
            .diskCacheStrategy(DiskCacheStrategy.ALL)

        Glide.with(context).load(path)
            .placeholder(R.drawable.icon_mine_avatar)
            .apply(mRequestOptions)
            .into(imageView)

    }

    /**
     * 加载图片
     */

    fun loadImage(context: Context, path: String, imageView: ImageView) {
        Glide
            .with(context)
            .load(path)
            .centerCrop()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .into(imageView)
    }


    /**
     * 加载头像
     */
    fun loadHeadCircleImg(context: Context, path: String, imageView: ImageView) {

        val mRequestOptions = RequestOptions
            .circleCropTransform()
            .dontAnimate()
            .placeholder(R.drawable.icon_mine_avatar)
        Glide
            .with(context).load(path)
            .apply(mRequestOptions)
            .into(imageView)
    }

    fun loadCover(context: Context, imageView: ImageView, filePath: String) {
        imageView.scaleType = ImageView.ScaleType.CENTER_CROP
        Glide.with(context)
            .setDefaultRequestOptions(
                RequestOptions()
                    .frame(4000000)
                    .centerCrop()
            )
            .load(filePath)
            .into(imageView)
    }

    fun loadThumbnail(
        context: Context,
        resize: Int,
        placeholder: Drawable?,
        imageView: ImageView,
        uri: Uri?
    ) {
        Glide.with(context)
            .asBitmap() // some .jpeg files are actually gif
            .load(uri)
            .apply(
                RequestOptions()
                    .override(resize, resize)
                    .placeholder(placeholder)
                    .centerCrop()
            )
            .into(imageView)
    }

    fun loadGifThumbnail(
        context: Context,
        resize: Int,
        placeholder: Drawable?,
        imageView: ImageView,
        uri: Uri
    ) {
        Glide.with(context)
            .asBitmap() // some .jpeg files are actually gif
            .load(uri)
            .apply(
                RequestOptions()
                    .override(resize, resize)
                    .placeholder(placeholder)
                    .centerCrop()
            )
            .into(imageView)
    }

    fun loadImage(
        context: Context,
        resizeX: Int,
        resizeY: Int,
        imageView: ImageView,
        uri: Uri
    ) {
        Glide.with(context)
            .load(uri)
            .apply(
                RequestOptions()
                    .override(resizeX, resizeY)
                    .priority(Priority.HIGH)
                    .fitCenter()
            )
            .into(imageView)
    }

    fun loadGifImage(
        context: Context,
        resizeX: Int,
        resizeY: Int,
        imageView: ImageView,
        uri: Uri
    ) {
        Glide.with(context)
            .asGif()
            .load(uri)
            .apply(
                RequestOptions()
                    .override(resizeX, resizeY)
                    .priority(Priority.HIGH)
                    .fitCenter()
            )
            .into(imageView)
    }


    /**
     * 加载聊天里面的图片
     */
    fun loadImImg(context: Context, path: String, imageView: ImageView) {

        Glide
            .with(context)
            .load(path)
            .placeholder(R.drawable.anim)
            .error(R.drawable.load_fail)
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .skipMemoryCache(false)
            .transform(CenterCrop(), GlideRoundTransform(8f))
            .into(imageView)
    }

}



